package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.support.hsf.HSFJSONUtils;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.google.gson.JsonObject;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public Map<String, Object> GoodsInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        Map<String,Object> map = new HashMap<>();
        List<Goods> goodsList = goodsDao.SelectAllGoods(page-1, rows, codeOrName, goodsTypeId);
        Integer count = goodsDao.SelectAllGoodsCount();
        goodsList.forEach(goods -> {
            if (goods.getSaleTotal()==null){
                goods.setSaleTotal(0);
            }
        });
        map.put("total", count);
        map.put("rows", goodsList);
        return map;
    }

    /**
     * 分页查询供应商
     * @param page
     * @param rows
     * @param supplierName
     * @return
     */
    @Override
    public Map<String, Object> supplierList(Integer page, Integer rows, String supplierName) {
        Map<String,Object> map = new HashMap<>();
        List<Supplier> supplierList = goodsDao.SelectAllSupplier(page-1, rows, supplierName);
        Integer count = goodsDao.SelectAllSupplierCount();
        map.put("total", count);
        map.put("rows", supplierList);
        return map;
    }

    @Override
    public ServiceVO supplierSaveAndUpdate(Supplier supplier) {
        if(supplier.getSupplierId() == null){
            goodsDao.addSupplier(supplier);
            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        }else{
            goodsDao.updateSupplier(supplier);
            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        }


    }

    /**
     * 删除供应商
     * @param ids
     * @return
     */
    @Override
    public ServiceVO supplierDelete(String ids) {
        String[] split = ids.split(",");
        List<String> strings = Arrays.asList(split);


        goodsDao.supplierDelete(strings);

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 分页查询客户
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @Override
    public Map<String, Object> customerList(Integer page, Integer rows, String customerName) {
        Map<String,Object> map = new HashMap<>();
        List<Customer> supplierList = goodsDao.SelectAllCustomer(page, rows, customerName);
        Integer count = goodsDao.SelectAllCustomerCount();

        map.put("total", count);
        map.put("rows", supplierList);
        return map;
    }

    /**
     * 添加或者修改客户
     * @param
     * @return
     */
    @Override
    public ServiceVO customerSaveAndUpdate(Customer customer) {
        if(customer.getCustomerId() == null){
            goodsDao.addCustomer(customer);
            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        }else{
            goodsDao.updateCustomer(customer);
            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        }
    }

    /**
     * 删除客户
     * @param ids
     * @return
     */
    @Override
    public ServiceVO customerDelete(String ids) {
        String[] split = ids.split(",");
        for (String id : split) {
            goodsDao.customerDelete(Integer.parseInt(id));
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }



    /**
     * 查询所有商品单位
     * @param
     * @return
     */
    @Override
    public Map<String, Object> listUnit() {
        Map<String,Object> map = new HashMap<>();
        List<Unit> goodsList = goodsDao.selectList();
        for (Unit unit : goodsList) {

            System.out.println(unit);
        }
        map.put("rows", goodsList);
        return map;
    }

    /**
     * 查询所有商品信息
     * @param page
     * @param rows
     * @param goodsName
     * @param goodsTypeId
     * @return
     */
    @Override
    public Map<String, Object> selectAllGoodsMsg(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        Map<String,Object> map = new HashMap<>();
        List<Goods> goodsList = goodsDao.selectAllGoodsMsg(page, rows, goodsName, goodsTypeId);
        Integer count = goodsDao.SelectAllGoodsCount();
        map.put("total", count);
        map.put("rows", goodsList);
        return map;
    }


    /**
     * 新增分类
     * @param goodsTypeName
     * @param pId
     * @return
     */
    @Override
    public ServiceVO saveGoodsType(String goodsTypeName, Integer pId) {

        goodsDao.saveGoodsType(goodsTypeName, pId);


        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 商品添加或修改
     * @param goods
     * @return
     */
    @Override
    public ServiceVO saveGoodsMsg(Goods goods) {
        if(goods.getGoodsId() == null){
            String uuid=UUID.randomUUID().toString().replaceAll("-","");
            goods.setGoodsCode(uuid);
            goodsDao.saveGoodsMsg(goods);
            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        }else{
            goodsDao.updateGoodsMsg(goods);
            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        }
    }

    /**
     *商品类别
     *
     * @param parentId
     * @return
     */
    @Override
    public List<GoodsType> findByParentId(Integer parentId) {


        return goodsDao.selectGoodsTypeAll(parentId);
    }

    /**
     * 删除分类
     * @param goodsTypeId
     * @return
     */
    @Override
    public ServiceVO deleteGoodsType(Integer goodsTypeId) {

            goodsDao.deleteGoodsType(goodsTypeId);

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);

    }

    /**
     * 删除商品信息
     * @param goodsId
     * @return
     */
    @Override
    public ServiceVO deleteGoodsMsg(Integer goodsId) {

        goodsDao.deleteGoodsMsg(goodsId);

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 分页查询无库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @Override
    public Map<String, Object> selectNullRepertory(Integer page, Integer rows, String nameOrCode) {
        Map<String,Object> map = new HashMap<>();
        List<Goods> goodsList = goodsDao.selectNullRepertory(page, rows, nameOrCode);
        Integer count = goodsDao.selectNullRepertoryCount();
        map.put("total", count);
        map.put("rows", goodsList);
        return map;

    }

    /**
     * 分页查询库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @Override
    public Map<String, Object> selectRepertory(Integer page, Integer rows, String nameOrCode) {
        Map<String,Object> map = new HashMap<>();
        List<Goods> goodsList = goodsDao.selectRepertory(page, rows, nameOrCode);
        Integer count = goodsDao.selectRepertoryCount();
        map.put("total", count);
        map.put("rows", goodsList);
        return map;
    }

    /**
     *
     * @param goodsId 商品ID
     * @param inventoryQuantity 库存
     * @param purchasingPrice 成本价
     * @return
     */
    @Override
    public ServiceVO addGoodsRepertory(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {


            goodsDao.addGoodsRepertory(goodsId,inventoryQuantity,purchasingPrice);
            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);



    }

    /**
     * 删除库存
     * @param goodsId
     * @return
     */
    @Override
    public ServiceVO deleteGoodsRepertory(Integer goodsId) {
        goodsDao.deleteGoodsRepertory(goodsId);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 新增报损
     * @param damageList
     * @param damageListGoodsStr
     * @return
     */
    @Override
    public ServiceVO saveDamageList(DamageList damageList, String damageListGoodsStr) {
        goodsDao.saveDamageList(damageList, damageListGoodsStr);


        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 新增报溢单
     * @param overflowList
     * @param overflowListGoodsStr
     * @return
     */
    @Override
    public ServiceVO saveOverflowListGoods(OverflowList overflowList, String overflowListGoodsStr) {
        goodsDao.saveOverflowListGoods(overflowList, overflowListGoodsStr);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 库存报警
     * @return
     */
    @Override
    public Map<String, Object> selectListAlarm() {
        Map<String,Object> map = new HashMap<>();
        List<Goods> goodsList= goodsDao.selectListAlarm();
        map.put("rows",goodsList);
        return map;
    }

    /**
     * 报损单查询
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public Map<String, Object> selectDamageListGoods(String sTime, String eTime) {

        Map<String,Object> map = new HashMap<>();
        List<DamageList> list =  goodsDao.selectDamageListGoods(sTime,eTime);
        map.put("rows",list);
        return map;
    }

    @Override
    public Map<String, Object> selectOverflowListGoods(String sTime, String eTime) {
        Map<String,Object> map = new HashMap<>();
        List<OverflowList> list =  goodsDao.selectOverflowListGoods(sTime,eTime);
        map.put("rows",list);
        return map;
    }

    @Override
    public Map<String, Object> selectDamageListGoodsList(Integer damageListId) {
        Map<String,Object> map = new HashMap<>();
        List<DamageList> list =  goodsDao.selectDamageListGoodsList(damageListId);
        map.put("rows",list);
        return map;
    }

    @Override
    public Map<String, Object> selectOverflowListGoodsList(Integer overflowListId) {
        Map<String,Object> map = new HashMap<>();
        List<DamageList> list =  goodsDao.selectOverflowListGoodsList(overflowListId);
        map.put("rows",list);
        return map;
    }


}
